<template>
    <div class="table-all">
        <!-- 搜索区 -->
        <div class="app-container search-box">
            <SearchForm
                    :form="searchData.form"
                    :options="searchData.options"
                    @searchUpdate="search"
                    @resetSearchData="resetSearchData"
            ></SearchForm>
        </div>
        <!-- 按钮区 -->
        <div class="app-container table-box">
            <div class="table-main">
                <!-- 表格 -->
                <v-table
                      title="隐患归档信息"
                        v-if="tableMainHeight>0"
                        :table-columns="tableColumns"
                        :table-data="tableData"
                        :loading="loading"
                        :is-index="true"
                        ref="table"
                        :maxHeight="tableMainHeight">
                    <template #topButtonList>
                        <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
                    </template>
                    <template #state="scope">
                      {{getDictValue(scope.row.state, stateOptions)}}
                    </template>
                    <template #bclassify="scope">
                      {{getDictValue(scope.row.bclassify, bClassifyOptions)}}
                    </template>
                    <template #sclassify="scope">
                      {{$commonJs.getRiskHazardLabelName(scope.row.bclassify, scope.row.sclassify, hazardBClassifyList)}}
                      <!--{{getDictValue(scope.row.sclassify, sClassifyOptions)}}-->
                    </template>
                    <template #category="scope">
                      {{getDictValue(scope.row.category, categoryOptions)}}
                    </template>
                    <template #grade="scope">
                      {{getDictValue(scope.row.grade, gradeOptions)}}
                    </template>
                    <template #superviseState="scope">
                      {{getDictValue(scope.row.superviseState, superviseStateOptions)}}
                    </template>

                    <template #operation="scope">
                        <a v-hasPermission="['riskHazard:add']" class="table-btn-view table-operation"
                           @click="handleEdit(scope.row)">{{$t('table.view')}}</a>
                        <!--
                        <i v-hasPermission="['riskHazard:delete']" class="el-icon-delete table-operation" style="color: #ED6F6F;"
                           @click="singleDelete(scope.row)"></i>
                         -->
                    </template>
                </v-table>
            </div>
            <div class="table-page">
                <!-- 页码 -->
                <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                            @pagination="search"/>
            </div>
        </div>
        <!-- 详情 -->
        <detailDialog :dialog-visible="detailDialogShow"
                    @success="editSuccess"
                    @close="editClose"
                    :superviseOrderNum="curSuperviseOrderNum"
                    :openType="detailOpenType"
                    :id="detailId"
                    :title="detailTitle"
        ></detailDialog>

    </div>
</template>

<script>
    import { riskHazardPage,
            riskHazardExport,
            riskHazardDelete } from '@/api/risk/riskHazard'
    import SearchForm from '@/components/searchForm/index'
    import ButtonForm from '@/components/ButtonForm/index'
    import VTable from '@/components/VTable/index'
    import Pagination from '@/components/Pagination/index'
    import detailDialog from './detail.vue'
    import tableImportDialog from '@/components/TableImportDialog/tableImportDialog'
    import { treeDataList} from '@/api/risk/treeData'
    export default {
        components:{
            VTable,Pagination,detailDialog,SearchForm,ButtonForm,tableImportDialog
        },
        data(){
            return{

              hazardBClassifyList:[],
                tableColumns:[
                            // {
                            //     prop: 'flagCode',
                            //     label: '标识码',
                            //     minWidth: '100',
                            //     slotname: 'flagCode',
                            //     options: {}
                            // },
                            {
                              prop: 'projectName',
                              label: '所属工地',
                              minWidth: '150',
                              slotname: 'projectName',
                              options: {}
                            },
                            {
                                prop: 'name',
                                label: '风险隐患名称',
                                minWidth: '150',
                                slotname: 'name',
                                options: {}
                            },

                            {
                                prop: 'bclassify',
                                label: '风险隐患大类',
                                minWidth: '150',
                                slotname: 'bclassify',
                                options: {}
                            },
                            {
                                prop: 'sclassify',
                                label: '风险隐患细类',
                                minWidth: '150',
                                slotname: 'sclassify',
                                options: {}
                            },

                            {
                              prop: 'grade',
                              label: '风险隐患等级',
                              minWidth: '150',
                              slotname: 'grade',
                              options: {}
                            },

                            {
                              prop: 'state',
                              label: '状态',
                              minWidth: '80',
                              slotname: 'state',
                              options: {}
                            },




                            {
                                prop: 'address',
                                label: '风险隐患地址',
                                minWidth: '150',
                                slotname: 'address',
                                options: {}
                            },




                            {
                                prop: 'reportName',
                                label: '上报人',
                                minWidth: '150',
                                slotname: 'reportName',
                                options: {}
                            },

                            {
                              prop: 'reportTime',
                              label: '上报时间',
                              minWidth: '150',
                              slotname: 'reportTime',
                              options: {}
                            },
                            {
                              prop: 'handleUnit',
                              label: '处置单位',
                              minWidth: '150',
                              slotname: 'handleUnit',
                              options: {}
                            },
                            {
                              prop: 'handleName',
                              label: '处置单位负责人',
                              minWidth: '150',
                              slotname: 'handleName',
                              options: {}
                            },
                            {
                              prop: 'handleCount',
                              label: '整改次数',
                              minWidth: '100',
                              slotname: 'handleCount',
                              options: {}
                            },
                            {
                              prop: 'handleTime',
                              label: '完成整改日期',
                              minWidth: '150',
                              slotname: 'handleTime',
                              options: {}
                            },
                            {
                              prop: 'finishTimeState',
                              label: '按时完成整改',
                              minWidth: '150',
                              slotname: 'finishTimeState',
                              options: {}
                            },
                            {
                              prop: 'superviseState',
                              label: '督办状态',
                              minWidth: '100',
                              slotname: 'superviseState',
                              options: {}
                            },



                    {
                        prop: 'operation',
                        label: '操作',
                        width: '200',
                        align: 'center',
                        slotname: 'operation',
                        overflow: false,
                        fixed: 'right'
                    }
                ],
                tableData:[],
                selection: [],
                loading:false,
                total:1,
                pageNum:1,
                pageSize:10,
                detailOpenType:"add",
                detailDialogShow:false,
                curSuperviseOrderNum:"",
                detailId:"",
                detailTitle:"新增",
                dict:{},
                searchData:{
                  form:{
                        state:'',
                        name:'',
                        bclassify:'',
                        address:'',
                        finishTimeState:''
                },
                  options:[

                             {
                                 title:'风险隐患名称',
                                 key: "name",
                                 type: "text",
                             },

                             {
                                 title:'风险隐患地址',
                                 key: "address",
                                 type: "text",
                             },
                             {
                               title:'风险隐患大类',
                               key: "bclassify",
                               type: "select",
                               multiple:false,
                               option: {
                                 data: this.bClassifyOptions,
                                 props: {
                                   key: "id", // 指定key对应data的字段
                                   label: "label", // 指定label对应data的字段
                                   value: "name", // 指定value对应data的字段
                                 },
                               },
                             },
                              {
                                title:'按时完成整改',
                                key: "finishTimeState",
                                type: "select",
                                multiple:false,
                                option: {
                                  data: this.finishTimeStateOptions,
                                  props: {
                                    key: "id", // 指定key对应data的字段
                                    label: "label", // 指定label对应data的字段
                                    value: "name", // 指定value对应data的字段
                                  },
                                },
                              },


                   ]
                },
                buttonList:[
                    //{name:'新增',methods:'handleAdd',type:'primary'},
                    //{name:'导入',methods:'handleImport'},
                    //{name:'导出',methods:'handleExport'}
                ],
                tableMainHeight: 0,
                // 字典
                stateOptions:[],
                bClassifyOptions:[],
                bSlassifyOptions:[],
                categoryOptions:[],
                gradeOptions:[],
                finishTimeStateOptions:[],
                superviseStateOptions:[],

            }
        },
        watch: {
            // 字典

            'stateOptions':{
              handler(newVal, oldVal) {
                this.searchData.options.forEach(t=>{
                  if(t.key=='state'){
                    t.option.data = newVal
                  }
                })
              },
              deep: true,
              immediate: true
            },

            'bClassifyOptions':{
                handler(newVal, oldVal) {
                    this.searchData.options.forEach(t=>{
                        if(t.key=='bclassify'){
                        t.option.data = newVal
                    }
                })
                },
                deep: true,
                immediate: true
            },

            'sClassifyOptions':{
              handler(newVal, oldVal) {
                this.searchData.options.forEach(t=>{
                  if(t.key=='sclassify'){
                    t.option.data = newVal
                  }
                })
              },
              deep: true,
              immediate: true
            },

          'categoryOptions':{
            handler(newVal, oldVal) {
              this.searchData.options.forEach(t=>{
                if(t.key=='category'){
                  t.option.data = newVal
                }
              })
            },
            deep: true,
            immediate: true
          },

          'gradeOptions':{
            handler(newVal, oldVal) {
              this.searchData.options.forEach(t=>{
                if(t.key=='grade'){
                  t.option.data = newVal
                }
              })
            },
            deep: true,
            immediate: true
          },

          'finishTimeStateOptions':{
            handler(newVal, oldVal) {
              this.searchData.options.forEach(t=>{
                if(t.key=='finishTimeState'){
                  t.option.data = newVal
                }
              })
            },
            deep: true,
            immediate: true
          },




        },
        methods:{
            // 判断是否显示图片
            checkImgShow(value){
              if (value == null || value == '' || value=='null') {
                return false;
              }
              let img = JSON.parse(value)
              return img.length > 0 ? true : false
            },
            // 格式化图片值
            getImgFormatValue(value, type){
              let img = JSON.parse(value)
              if(type=='url'){
                // 只显示第一张
                return process.env.VUE_APP_BASE_API + 'file/api/download/'+ img[0].fileId
              }
              if(type=='list'){
                return img.map(t=>{ return process.env.VUE_APP_BASE_API + 'file/api/download/'+ t.fileId})
                // return [process.env.VUE_APP_BASE_API + 'file/api/download/'+ img[0].fileId]
              }
            },

            handleClose() {
              this.importDialog.isVisible = false
            },

            getCheckBoxValue(value,option){
                if(!value){
                    return
                }
                let temp = []
                let data = value.split(",")
                data.forEach(t=>{
                    let item = option.find(x=> x.value == t)
                if(item != undefined){
                    temp.push(item.label)
                }
            })
                return temp.join(",")
            },
            getDictValue(value,option){
                let item = option.find(t=> t.value == value)
                if(item!=undefined){
                    return item.label
                }
                return value
            },
            editClose(){
                this.detailDialogShow = false
            },
            editSuccess(){
                this.search()
                this.detailDialogShow = false
            },
            search(){
                this.fetch({
                        ...this.searchData.form,
                })
            },
            fetch(params={}){
                params.pageSize = this.pageSize
                params.pageNum = this.pageNum
                //查询 已归档
                params.state=3;
                //params.areaCode = this.$store.state.account.user.deptIds
                this.loading = true
                 riskHazardPage(params).then(res=>{
                    if(res.code==200){
                        this.total = res.data.total
                        this.tableData = res.data.rows
                    }else{
                    this.$message({
                        message: this.$t('tips.systemException'),
                        type: 'error'
                     })
                   }
                }).finally(final=>{
                    this.loading = false
               })
            },
            // 重置搜索条件
            resetSearchData(){
                this.searchData.form = {
                        state:'',
                        name:'',
                        bclassify:'',
                        address:'',
                        finishTimeState:''

                }
            },

            // 触发按钮区域按钮方法
            clickButton(methods){
                this[methods]()
            },
            handleAdd(){
                this.detailOpenType = "add"
                this.detailTitle = "新增"
                this.detailDialogShow = true
            },
            handleEdit(row){
                this.detailId = row.id
                this.detailOpenType = "edit"
                this.detailTitle="详情"
                this.detailDialogShow = true
                this.curSuperviseOrderNum= row.superviseOrderNum
               console.log("this.curSuperviseOrderNum==",this.curSuperviseOrderNum)
            },



            closeDialog(){
              this.detailDialogShow = false
            },

            singleDelete(row){
                this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
                    confirmButtonText: this.$t('common.confirm'),
                    cancelButtonText: this.$t('common.cancel'),
                    type: 'warning'
                }).then(() => {
                    this.loading = true
                    riskHazardDelete(row.id).then(res=>{
                    if(res.code==200){
                    this.$message({
                        message: this.$t('tips.deleteSuccess'),
                        type: 'success'
                    })
                    this.search()
                     }else{
                        this.$message({
                            message: this.$t('tips.systemException'),
                            type: 'error'
                        })
                    }
                 }).finally(final=>{
                    this.loading = false
                 })
                })
            }
        },
        activated(){
            this.fetch()
        },
        created(){
            // 获取字典值

          this.stateOptions = []
          this.$commonJs.filterDataDict("hazardDealState",this.stateOptions )

          this.bClassifyOptions = []
          this.$commonJs.filterDataDict("hazardBClassify",this.bClassifyOptions )

          this.sClassifyOptions = []
          this.$commonJs.filterDataDict("hazardSClassify",this.sClassifyOptions )

          this.categoryOptions = []
          this.$commonJs.filterDataDict("hazardCategory",this.categoryOptions )

          this.gradeOptions = []
          this.$commonJs.filterDataDict("patrolHazardLevel",this.gradeOptions )

          this.finishTimeStateOptions = [{"name":"1","label":"按时"},{"name":"2","label":"超时"}]
          this.superviseStateOptions = []
          this.$commonJs.filterDataDict("superviseState",this.superviseStateOptions )
          treeDataList({name:"hazardBClassify"}).then(res=>{
            if(res.code==200){
              this.hazardBClassifyList = res.data
            }
          })

        },
        mounted(){
            this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
        }
    }
</script>

<style lang="scss" scoped>
    .table-all {
        display: flex;
        flex-direction: column;
        height: 100%;

    .search-box {
        padding-bottom: 6px;
    }

    .table-box {
        padding-top: 10px;
        flex: 1;
        display: flex;
        flex-direction: column;

    .table-main {
        flex: 1;
        overflow: auto;
    }

    .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
    }

    ::v-deep .pagination-container {
        text-align: right;
    }

    }
    }
</style>
